Dir_data=$1
cd $Dir_data

if test ! -r Zero.fit
then
	echo * | xargs -n 1 | grep  "bias" >aaa
	if test -s aaa
	then
		Dir_bias=`echo * | xargs -n 1 | grep  "bias" `
		cd $Dir_bias
		ls *.fit >listbias
		xbiascom.sh listbias
		cp Zero.fit ../
		cd ../
	fi
fi
if test ! -r Flat*.fit
then
	echo * | xargs -n 1 | grep  "flat" >aaa
	if test -s aaa
	then
		if test -r Zero.fit 
		then
			Dir_flat=`echo * | xargs -n 1 | grep  "flat" `
			cp Zero.fit $Dir_flat
			cd $Dir_flat
			ls *R*.fit | grep -v "Zero.fit" >listflat
			xzerocorr_list.sh listflat
			xflatcom.sh listflat
			Flatimg=`ls Flat*.fit`
			if test -s $Flatimg
			then
				cp  $Flatimg ../
			fi	
			cd ../
			echo "flatcom is finished!"
			
		fi
	fi
fi

echo * | xargs -n 1 | grep -v "bias"  | grep -v "flat" | grep -v "aaa" | grep -v "reduc" | grep -v "Zero" | grep -v "list" | grep -v "xtime" | grep -v "Flat" | grep -v "G1" | grep -v "test" | awk '{print(Dirdata"/"$1)}' Dirdata=$Dir_data >listdir
cat listdir | grep -v "listdir" >tmp
mv tmp listdir
if test -s listdir.bak
then
	sort listdir.bak >tmp
	mv tmp listdir.bak
	diff listdir.bak listdir | grep  ">" | tr -d '>' | column -t >listmatch
	mv listmatch listdir
fi
cat listdir
if test -s listdir_real
then
	rm listdir_real
fi
for Dir_singleobj in `cat listdir`
do
	echo $Dir_singleobj
	cd $Dir_singleobj
	Nimg=`ls *.fit | wc -l | awk '{print($1)}'`
	if [ $Nimg  -ge 3  ]
	then
		cd $Dir_data
		Flatimg=`ls Flat*.fit`
		cp Zero.fit $Flatimg $Dir_singleobj
		echo $Dir_singleobj >>listdir_real
	fi
	#cd $Dir_singleobj
done

rm -rf listdir
sleep 3
cd $Dir_data
xzeroflatcorr_listdir.sh listdir_real
cd $Dir_data
xauto_60cm_list.sh listdir_real
wait

cat listdir_real >>listdir.bak
